package com.miao.listview;

import android.os.Bundle;
import android.app.ListActivity;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.miao.listview.R;

import java.util.ArrayList;
import java.util.Arrays;

public class DragDemo extends ListActivity {
	private static String[] items = { "浙江", "江苏", "广东", "上海","未定阅", "北京", "新疆","天津","山东","辽宁","甘肃","黑龙江" };
	private IconicAdapter adapter = null;
	private ArrayList<String> array = new ArrayList<String>( Arrays.asList(items));
	private ArrayList<String> afterArray=new ArrayList<String>();

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.main);

		adapter = new IconicAdapter();
		setListAdapter(adapter);

		DraggableListView tlv = (DraggableListView) getListView();

		tlv.setDropListener(onDrop);
		tlv.setRemoveListener(onRemove);
		tlv.getAdapter();
	}

	private DraggableListView.DropListener onDrop = new DraggableListView.DropListener() {
		@Override
		public void drop(int from, int to) {
			String item = adapter.getItem(from);

			adapter.remove(item);
			adapter.insert(item, to);
			Toast.makeText(DragDemo.this, adapter.getList().toString(),
					Toast.LENGTH_SHORT).show();
			afterArray=adapter.getList();  //保存变化后的排序
		}
	};

	private DraggableListView.RemoveListener onRemove = new DraggableListView.RemoveListener() {
		@Override
		public void remove(int which) {
			adapter.remove(adapter.getItem(which));
		}
	};

	class IconicAdapter extends ArrayAdapter<String> {
		IconicAdapter() {
			super(DragDemo.this, R.layout.row, array);
		}
		public ArrayList<String> getList() {
			return array;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			View row = convertView;
			if (row == null) {
				LayoutInflater inflater = getLayoutInflater();
				row = inflater.inflate(R.layout.row, parent, false);
			}
			row.findViewById(R.id.icon).setVisibility(View.VISIBLE);
			((TextView)row.findViewById(R.id.label)).setGravity(Gravity.LEFT);
			((TextView)row.findViewById(R.id.label)).setBackgroundColor(Color.BLACK);
			((TextView)row.findViewById(R.id.label)).setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
			if(array.get(position).equals("已订阅")||array.get(position).equals("未定阅")) {
			    row.findViewById(R.id.icon).setVisibility(View.GONE);
			    ((TextView)row.findViewById(R.id.label)).setBackgroundColor(Color.parseColor("#eeeeee"));
			    ((TextView)row.findViewById(R.id.label)).setGravity(Gravity.CENTER);
			    ((TextView)row.findViewById(R.id.label)).setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
			}
			TextView label = (TextView) row.findViewById(R.id.label);
			label.setText(array.get(position));
			return (row);
		}
	}
}